package com.knowledge.plus.other.执行顺序;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 执行顺序：
 * 静态代码块 -> 普通代码块 -> Constructor(构造方法) -> @Autowired -> @PostConstruct —> @PreDestroy
 *
 * @author PF.Tian
 * @since 2022/4/2
 */
@RestController
public class OrderDemoController {

    @Autowired
    private ChildrenDemo children;

    @GetMapping("order")
    public void order() {
        children.commonFun();
    }

}

@Data
@Service
class FatherDemo {

    static {
        System.out.println("Order Father 静态代码块");
    }

    {
        System.out.println("Order Father 普通代码块");
    }

    public FatherDemo() {
        System.out.println("Order Father 构造方法");
    }

    @PostConstruct
    public void init() {
        System.out.println("Order Father @PostConstruct");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Order Father @PreDestroy");
    }

    public static void staticFun() {
        System.out.println("Order Father 静态方法");
    }

    public void commonFun() {
        System.out.println("Order Father 普通方法");
    }

}


@Data
@Service
class ChildrenDemo {
    private String name = "ZhangSan";
    private int age = 18;

    static {
        System.out.println("Order Children 静态代码块");
    }

    {
        System.out.println("Order Children 普通代码块");
    }


    public ChildrenDemo() {
        System.out.println("Order Children 构造方法");
    }

    @PostConstruct
    public void init() {
        System.out.println("Order Children @PostConstruct");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Order Children @PreDestroy");
    }

    public static void staticFun() {
        System.out.println("Order Children 静态方法");
    }

    public void commonFun() {
        System.out.println("Order Children 普通方法");
    }

}
